package com.example.s.Activity.login;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.R;

public class ForgetPasswordActivity extends BaseActivity {
    private ImageView back;
    private EditText email;
    private EditText identifyCode;
    private TextView getIdentifyCode;
    private EditText password;
    private EditText confirmPassword;
    private ImageView seePwd;
    private TextView confirm;
    private int codeLimitTime = 60;
    private Handler mHandler;
    boolean isSee = false;

    private void initView() {


        back = (ImageView) findViewById(R.id.back);
        email = (EditText) findViewById(R.id.email);
        identifyCode = (EditText) findViewById(R.id.identifyCode);
        getIdentifyCode = (TextView) findViewById(R.id.getIdentifyCode);
        password = (EditText) findViewById(R.id.password);
        confirmPassword = (EditText) findViewById(R.id.confirmPassword);
        seePwd = (ImageView) findViewById(R.id.seePwd);
        confirm = (TextView) findViewById(R.id.confirm);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        initView();
        mHandler = new Handler(Looper.getMainLooper());
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getIdentifyCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.post(mGetCodeTask);
                getIdentifyCode.setEnabled(false);
            }
        });
        confirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >= 5) {
                    seePwd.setVisibility(View.VISIBLE);
                } else {
                    seePwd.setVisibility(View.INVISIBLE);
                }
            }
        });

        seePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: " + isSee);
                if (!isSee) {
                    password.setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());
                    confirmPassword.setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());  //密码以明文显示
                    v.setBackgroundResource(R.mipmap.eye_icon_close);
                    isSee = true;
                } else {
                    password.setTransformationMethod(PasswordTransformationMethod
                            .getInstance());
                    confirmPassword.setTransformationMethod(PasswordTransformationMethod
                            .getInstance());  //以密文显示，以.代替
                    v.setBackgroundResource(R.mipmap.eye_icon);
                    isSee = false;
                }
            }
        });
    }


    private Runnable mGetCodeTask = new Runnable() {
        @Override
        public void run() {
            if (codeLimitTime <= 0) {
                mHandler.removeCallbacks(mGetCodeTask);
                getIdentifyCode.setText("获取验证码");
                getIdentifyCode.setEnabled(true);
                codeLimitTime = 60;
                return;
            }
            getIdentifyCode.setText(--codeLimitTime + "s");
            mHandler.postDelayed(mGetCodeTask, 1000);
        }
    };
}
